package com.example.vm.service.impl;

import com.common.holder.UserHolder;
import com.common.utils.IDUtils;
import com.example.api.client.UserClient;
import com.example.vm.domain.entity.VideoComment;
import com.example.vm.domain.entity.VideoLike;
import com.example.vm.domain.entity.VideoShare;
import com.example.vm.mapper.VideoLikeMapper;
import com.example.vm.mapper.VideoShareMapper;
import com.example.vm.service.ActionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Service
public class ActionServiceImpl implements ActionService {
    @Autowired
    private VideoShareMapper videoShareMapper;
    @Autowired
    private VideoLikeMapper videoLikeMapper;
    @Autowired
    private UserClient userClient;
    @Override
    public void shareVideo(Long id)  {
        Long userId = UserHolder.getUserId();
        InetAddress localHost = null;
        try {
            localHost = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
        String hostAddress = localHost.getHostAddress();
        VideoShare videoShare =videoShareMapper.getByUserIdAndVideoIdAndIP(userId,id,hostAddress);
        if(ObjectUtils.isEmpty(videoShare)){
            VideoShare share = new VideoShare();
            share.setShartNum(1);
            share.setVideoId(id);
            share.setUserId(userId);
            share.setIp(hostAddress);
            videoShareMapper.insert(share);
        }else{
            throw new RuntimeException("一个用户ip只能分享一次哦");
        }
    }

    @Override
    public void commentVideo(VideoComment videoComment) {
        
    }

    @Override
    public void collectVideo(Long collectId,Long videoId) {
        userClient.collectVideo(collectId,videoId);
    }

    @Override
    public void likeVideo(Long id) {
        Long userId = UserHolder.getUserId();
        VideoLike videoLike = new VideoLike();
        videoLike.setVideoId(id);
        videoLike.setUserId(userId);
        videoLikeMapper.save(videoLike);
    }

}
